package com.learning.railwayreservation.action;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.learning.railwayreservation.biz.Booking;
import com.learning.railwayreservation.formbeans.ConfirmBookingForm;
import com.learning.railwayreservation.vo.PassengerVO;
import com.learning.railwayreservation.vo.SeatVO;
import com.learning.railwayreservation.vo.TicketVO;

public class ConfirmBookingAction extends Action {
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
// change something here
//		int counter = 1;
//
//		PassengerVO passengerVo;
//		List<PassengerVO> passList = new ArrayList<PassengerVO>();
//
//		System.out.println("Seat Id: "
//				+ request.getParameter("USeatId_" + counter));
//		while (request.getParameter("USeatId_" + counter) != null) {
//			passengerVo = new PassengerVO();
//			passengerVo.setFirstName(request.getParameter("FirstName_"
//					+ counter));
//			passengerVo
//					.setLastName(request.getParameter("LastName_" + counter));
//			passengerVo.setAge(new Integer(request.getParameter("Age_"
//					+ counter)).intValue());
//			passengerVo.setSex(request.getParameter("Sex_" + counter));
//			passengerVo.getSeatVo().setUniqueSeatId(
//					new Integer(request.getParameter("USeatId_" + counter))
//							.intValue());
//			System.out.println(" Name: " + passengerVo.getFirstName() + " "
//					+ passengerVo.getLastName());
//			System.out.println(" Age: " + passengerVo.getAge());
//			System.out.println(" Sex: " + passengerVo.getSex());
//			System.out.println(" Seat ID: "
//					+ passengerVo.getSeatVo().getUniqueSeatId());
//
//			passList.add(passengerVo);
//			counter++;
//		}
		
		Booking bookTickets = new Booking();
		TicketVO ticketvo = new TicketVO();
//		ticketvo.setPassengerList(passList);

		ConfirmBookingForm confirmTicketBooking = (ConfirmBookingForm) form;
		BeanUtils.copyProperties(confirmTicketBooking, ticketvo);
		System.out.println("---- " + ticketvo.getPassengerList());
		List<PassengerVO> passengerList = ticketvo.getPassengerList();
		for(PassengerVO passenger : passengerList){
			System.out.println("Unique Seat Id: "+ passenger.getSeatVo().getUniqueSeatId());
		}
		
		TicketVO confirmBooking = bookTickets.bookTickets(ticketvo);
		System.out.println(" Train Id "+confirmBooking.getTrainId());
		System.out.println(" Train Name "+confirmBooking.getTrainName());
		request.setAttribute("ticket", confirmBooking);
		return (mapping.findForward("success"));

	}

}
